package org.sump.mysql;

import org.sump.data.Grant;
import org.sump.data.HasGrants;
import org.sump.data.Permission;
import org.sump.data.Resource;

public class MySQLGrant implements Grant {

	private HasGrants subject;
	private Resource resource;
	private Permission permission;

	public MySQLGrant(HasGrants subject, Resource resource,
			Permission permission) {
		super();
		this.subject = subject;
		this.resource = resource;
		this.permission = permission;
	}

	public Permission getPermission() {
		return permission;
	}

	public Resource getResource() {
		return resource;
	}

	public HasGrants getSubject() {
		return subject;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((permission == null) ? 0 : permission.hashCode());
		result = prime * result
				+ ((resource == null) ? 0 : resource.hashCode());
		result = prime * result + ((subject == null) ? 0 : subject.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final MySQLGrant other = (MySQLGrant) obj;
		if (permission == null) {
			if (other.permission != null)
				return false;
		} else if (!permission.equals(other.permission))
			return false;
		if (resource == null) {
			if (other.resource != null)
				return false;
		} else if (!resource.equals(other.resource))
			return false;
		if (subject == null) {
			if (other.subject != null)
				return false;
		} else if (!subject.equals(other.subject))
			return false;
		return true;
	}

	public String toString() {
		return resource + " is granted for " + subject + " for " + permission;
	}

}
